package Dailog;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import base.BaseBean;
import bean.GoodsApply;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.ResponseBody;
import student.andlei.com.student.R;
import utils.GsonUtils;
import utils.ImageLoaderUtils;
import utils.NetUtils;

/**
 * author : ChenHuiMing
 * date : 2018/9/26
 * email : 907399255@qq.com
 * description : 支付Dialog
 */
public class PayDialog extends Dialog {

    public static final String SHOP_GETORDER_URL = "/Shop/getOrder";

    private ImageView mIvQrCode;
    private int mOrderId;
    private NetUtils mNetUtils;
    private Gson mGson;
    private ImageView mIvClose;


    public PayDialog(@NonNull Context context, String qrCodeUrl, int orderId) {
        super(context);
        mGson = new Gson();
        mNetUtils = new NetUtils(context);
        mOrderId = orderId;
        // 获取窗口管理器
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        setContentView(R.layout.dialog_pay);
        mIvQrCode = findViewById(R.id.iv_pay);
        mIvClose = findViewById(R.id.iv_close);
        ImageLoaderUtils.loadImage(context, qrCodeUrl, mIvQrCode);
        handler.sendEmptyMessage(1);
        mIvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }

    @Override
    public void dismiss() {
        super.dismiss();
        handler.removeCallbacksAndMessages(null);
    }


    private OnPayListener mOnPayListener;

    public void setOnPayListener(OnPayListener onPayListener) {
        mOnPayListener = onPayListener;
    }

    public interface OnPayListener {
        public void onPay(boolean isSucceed);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            Map<String, Object> map = new HashMap<>();
            map.put("order_id", mOrderId);
            mNetUtils.post(SHOP_GETORDER_URL, map, new Observer<ResponseBody>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(ResponseBody responseBody) {
                    try {
                        String responseString = responseBody.string();
                        BaseBean baseBean = mGson.fromJson(responseString, BaseBean.class);
                        GoodsApply mGoodsApply = (GoodsApply) GsonUtils.JSONToObject(GsonUtils.beanToJSONString(baseBean.data), GoodsApply.class);
                        if (100 == mGoodsApply.pay_status) {
                            if (mOnPayListener != null) {
                                mOnPayListener.onPay(true);
                            }
                            dismiss();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            });

            sendEmptyMessageDelayed(1, 1000);
        }
    };
}
